package it.lupo.core.gui.controller;

import org.apache.log4j.Logger;

import it.lupo.core.LupoSession;
import it.lupo.core.gui.MainApp;
import it.lupo.model.BarCode;
import it.lupo.model.service.BarCodeService;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class BarCodeController {
	
	public static Logger logger = Logger.getLogger(BarCodeController.class);

	public static String FXML_FILE = "src/main/resources/home-barcode.fxml";
	
	private BarCodeService service = BarCodeService.getInstance();
	
	@FXML
	private TextField code;
	
	
	@FXML
	private Label message;
	

	private BarCode bcode;
	
	@FXML
	public void deleteCode()
	{
		code.setText("");
	}
	
	@FXML
	public void onChange()
	{
		System.out.println(">> "+code.getText());
		bcode = service.check(code.getText());
		setTextField(bcode);
	}
	
	private void setTextField(BarCode code)
	{
		if(code!=null)
		{
//			form_surname.setText(code.getSurname());
//			form_name.setText(code.getName());
//			form_cf.setText(code.getCF());
//			form_address.setText(code.getAddress());
			onConferment();
			
		}else
		{
//			form_surname.setText("");
//			form_name.setText("");
//			form_cf.setText("");
//			form_address.setText("");
		}
		
	}
	
	@FXML
	public void onAdd()
	{
		logger.warn("DA IMPLEMENTARE");
	}
	
	@FXML
	public void onSearch()
	{
		logger.warn("DA IMPLEMENTARE");
		try {
			MainApp.getInstance().change(SearchController.FXML_FILE);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@FXML
	public void onExit()
	{
		LupoSession.getCurrent().logout();
		try {			
			MainApp.getInstance().change(LoginController.FXML_FILE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	@FXML
	public void onConferment()
	{
		if(bcode!=null){
			LupoSession.getCurrent().setBarcodeSession(bcode);
			try {
				MainApp.getInstance().change(HomeController.FXML_FILE);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else
		{
			message.setText("Inserisci un codice corretto");
		}
	}
}
